package com.winbaoxian.testng.platform.task;

import com.winbaoxian.testng.platform.model.dto.PlatformProjectDTO;
import com.winbaoxian.testng.platform.service.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class IntegrationProjectRunScheduler {

    @Resource
    private ProjectService projectService;

    @Scheduled(cron = "0 0 7 * * 1-5")
    public void workDayRun() {
        log.info("IntegrationProjectRunScheduler.workDayRun start");
        List<PlatformProjectDTO> projectDTOList = projectService.getByIntegrationFlagTrue();
        log.info("IntegrationProjectRunScheduler.workDayRun, integration project count:{}", CollectionUtils.size(projectDTOList));
        if (CollectionUtils.isNotEmpty(projectDTOList)) {
            for (PlatformProjectDTO projectDTO : projectDTOList) {
                log.info("IntegrationProjectRunScheduler.workDayRun, project[id:{}, name:{}] start run testCases", projectDTO.getId(), projectDTO.getName());
                try {
                    projectService.runForScheduler(projectDTO.getId());
                } catch (Exception e) {
                    log.error("IntegrationProjectRunScheduler.workDayRun, project[id:{}, name:{}] run error, msg:{}", projectDTO.getId(), projectDTO.getName(), e.getMessage(), e);
                }
            }
        }
        log.info("IntegrationProjectRunScheduler.workDayRun end");
    }

}